package br.urca.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.apache.commons.mail.SimpleEmail;

public class AOEmail {
	
	private static final String USER = "alunoonline@urca.br";
	private static final String PASS = "bUpA@U5ep_6n";

	public static void enviarEmail(final String host, final int porta,
			final String usuario, final String senha, final String remetente,
			final String destinatario, final String assunto,
			final String mensagem) throws EmailException {
		Email email = new SimpleEmail();
		email.setHostName(host);//"smtp.googlemail.com"
		email.setSmtpPort(porta);//465
		email.setAuthenticator(new DefaultAuthenticator(usuario,
				senha));
		email.setSSLOnConnect(true);
		email.setFrom(remetente);
		email.setSubject(assunto);
		email.setMsg(mensagem);
		email.addTo(destinatario);
		email.send();
	}
	
	public static void enviarHtmlEmail(final String destinatario,
			final String assunto, final String mensagem) throws EmailException {
		HtmlEmail email = new HtmlEmail();
		email.setHostName("smtp.googlemail.com");
		email.setSmtpPort(465);
		email.setAuthenticator(new DefaultAuthenticator(USER, PASS));
		email.setSSLOnConnect(true);
		email.setFrom(USER, "Aluno Online - URCA");
		email.setSubject(assunto);
		email.setHtmlMsg(mensagem);
		email.addTo(destinatario);
		email.send();
	}
	
	public static boolean validarEmail(String email) {
		boolean retorno = false;
		// Expressão Regular para validar E-mail
		Pattern p = Pattern
				.compile("^[\\w-]+(\\.[\\w-]+)*@([\\w-]+\\.)+[a-zA-Z]{2,7}$");
		Matcher m = p.matcher(email);
		if (!m.find())
			retorno = false;
		else
			retorno = true;
		return retorno;

	}
	
}
